package com.gxuwz.service.impl;

import com.gxuwz.dao.StudentDao;
import com.gxuwz.entity.Department;
import com.gxuwz.entity.Student;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.PageInfo;
import com.gxuwz.service.StudentService;
import com.gxuwz.util.IDCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    public PageVo getStudentList(Student student, PageInfo pageInfo,String instID) {
        if(student == null){
            return studentDao.selectStudentAll(null,pageInfo,instID);
        }
        String stuID = student.getStuID();
        String stuName = student.getStuName();
        String sex = student.getSex();
        String stuTel = student.getStuTel();
        if( (stuID!=null && !stuID.equals("")) || (stuName!= null && !stuName.equals("")) || (sex!=null && !sex.equals("") || (stuTel!=null && !stuTel.equals("")))){
            return studentDao.selectStudentAll(student, pageInfo,instID);
        } else {
            return studentDao.selectStudentAll(null,pageInfo,instID);
        }
    }

    public Student getStudentOne(String stuID) {
        Student student = studentDao.selectStudentByID(stuID);
        return student;
    }

    public boolean addStudentInfo(Student student) {
        student.setStuID(new IDCodeUtil().getStudentIDCode());
        int i = studentDao.insertStudent(student);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    public boolean updateStudentInfo(Student student, String oldId) {
        int i = studentDao.updateStudent(student,oldId);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    public boolean deleteStudentOne(String stuID) {
        int i = studentDao.deleteStudentById(stuID);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    public boolean deleteStudents(String[] ids) {
        int i = studentDao.deleteStudents(ids);
        boolean flag = i > 0 ? true : false;
        return flag;
    }

    @Override
    public boolean selectStudentExist(String classId) {
        List<Student> students = studentDao.selectStudentByClassesID(classId);
        if(students.isEmpty()){
            return true;
        } else {
            return false;
        }
    }
}
